﻿using System;
using System.Management;
using System.Security.Cryptography;
using System.Text;
using System.Windows;

namespace Trade.Journal
{
    /// <summary>
    /// FirstRunWindow.xaml 的交互逻辑
    /// </summary>
    public partial class FirstRunWindow : Window
    {
        public FirstRunWindow()
        {
            InitializeComponent();
        }

        private void CommandBinding_CanExecute(object sender, System.Windows.Input.CanExecuteRoutedEventArgs e)
        {
            if (DataContext is FirstRunWindowViewModel v && !string.IsNullOrWhiteSpace(v.Author) && v.Author.Length >= 2)
            {
                e.CanExecute = true;
            }
        }

        private void CommandBinding_Executed(object sender, System.Windows.Input.ExecutedRoutedEventArgs e)
        {
            if (DataContext is FirstRunWindowViewModel v)
            {
                DbHelper.SetConfigValue("Author", v.Author);
                if (!string.IsNullOrWhiteSpace(v.Vip))
                    DbHelper.SetConfigValue("Vip", v.Vip);
                DbHelper.SetConfigValue("Initialized", true);

                Close();
            }
        }
    }


    public class FirstRunWindowViewModel : BindableBase
    {
        private string _Author;

        public string Author
        {
            get { return _Author; }
            set { SetProperty(ref _Author, value); }
        }

        public string MachineCode { get; } = MachineInfo.MachineCode;


        private string _Vip;

        public string Vip
        {
            get { return _Vip; }
            set { SetProperty(ref _Vip, value); }
        }

    }
    public static class MachineInfo
    {
        private static string _MachineCode;

        public static string MachineCode
        {
            get
            {
                if (string.IsNullOrWhiteSpace(_MachineCode))
                    _MachineCode = GetMachineCodeString();
                return _MachineCode;
            }
        }


        static string StringToMD5Hash(string inputString)
        {
            using SHA256 sha = SHA256.Create();
            var encryptedBytes = sha.ComputeHash(Encoding.ASCII.GetBytes(inputString));
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < encryptedBytes.Length; i++)
            {
                sb.AppendFormat("{0:x2}", encryptedBytes[i]);
            }
            return sb.ToString();
        }

        public static string GetMachineCodeString()
        {
            var machineCodeString = GetCpuInfo() + GetHDid() + GetMoAddress();
            return StringToMD5Hash(machineCodeString);
        }

        ///   <summary> 
        ///   获取cpu序列号     
        ///   </summary> 
        ///   <returns> string </returns> 
        private static string GetCpuInfo()
        {
            string cpuInfo = "";
            try
            {
                using (ManagementClass cimobject = new ManagementClass("Win32_Processor"))
                {
                    ManagementObjectCollection moc = cimobject.GetInstances();

                    foreach (ManagementObject mo in moc)
                    {
                        cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
                        mo.Dispose();
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
            return cpuInfo.ToString();
        }

        ///   <summary> 
        ///   获取硬盘ID     
        ///   </summary> 
        ///   <returns> string </returns> 
        private static string GetHDid()
        {
            string HDid = "";
            try
            {
                using (ManagementClass cimobject1 = new ManagementClass("Win32_DiskDrive"))
                {
                    ManagementObjectCollection moc1 = cimobject1.GetInstances();
                    foreach (ManagementObject mo in moc1)
                    {
                        HDid = (string)mo.Properties["Model"].Value;
                        mo.Dispose();
                    }
                }
            }
            catch (Exception)
            {

                throw;
            }
            return HDid.ToString();
        }

        ///   <summary> 
        ///   获取网卡硬件地址 
        ///   </summary> 
        ///   <returns> string </returns> 
        private static string GetMoAddress()
        {
            string MoAddress = "";
            try
            {
                using (ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"))
                {
                    ManagementObjectCollection moc2 = mc.GetInstances();
                    foreach (ManagementObject mo in moc2)
                    {
                        if ((bool)mo["IPEnabled"] == true)
                            MoAddress = mo["MacAddress"].ToString();
                        mo.Dispose();
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
            return MoAddress.ToString();
        }
    }
}
